<template>
  <div class="ws-channel">
    <el-row v-show="ordin">
      <ws-ordinary @updateState="handleUpdateState"></ws-ordinary>
      <el-row>
        <el-col :span="23" :offset="1">
          <el-button type="primary" size="small" @click="handleClickOnSubmit(200, false)">通过并提交审核</el-button>
          <el-button type="danger" size="small" @click="handleClickOnSubmit(-200, false)">不通过</el-button>
        </el-col>
      </el-row>
    </el-row>
    <el-row v-show="activi">
      <ws-activity @updateState="handleUpdateState"></ws-activity>
      <el-row>
        <el-col :span="23" :offset="1">
          <el-button type="primary" size="small" @click="handleClickOnSubmit(200, true)">通过并提交审核</el-button>
          <el-button type="danger" size="small" @click="handleClickOnSubmit(-200, true)">不通过</el-button>
        </el-col>
      </el-row>
    </el-row>
  </div>
</template>

<script>
import { verfiryChannelWsValid } from '@/api/shop'
import WsOrdinary from './components/ws-channel/ordinary'
import WsActivity from './components/ws-channel/activities'

export default {
  components: {
    WsOrdinary,
    WsActivity
  },
  data() {
    return {
      ordin: false,
      activi: false
    }
  },
  methods: {
    handleClickOnSubmit(status, is_blue) {
      if (status === -200) {
        this.$msgbox({
          title: '拒绝原因',
          showInput: true,
          inputType: 'textarea',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true
        }).then(({ value }) => {
          verfiryChannelWsValid(this.$route.query.id, status, is_blue, value).then(res => {
            if (res.data.errorCode === 0) {
              if (is_blue) {
                this.activi = false
              } else {
                this.ordin = false
              }
              this.$message.success(res.data.errorMsg)
              this.handleWatchStatusUpdate()
            } else {
              this.$message.error(res.data.errorMsg)
            }
          })
        }).catch(() => {
          return false
        })
      } else {
        verfiryChannelWsValid(this.$route.query.id, status, is_blue, '').then(res => {
          if (res.data.errorCode === 0) {
            if (is_blue) {
              this.activi = false
            } else {
              this.ordin = false
            }
            this.$message.success(res.data.errorMsg)
            this.handleWatchStatusUpdate()
          } else {
            this.$message.error(res.data.errorMsg)
          }
        })
      }
    },
    handleUpdateState(arr) {
      const [type, state] = arr
      if (type === 'ordin') this.ordin = state
      if (type === 'activi') this.activi = state
    },
    handleWatchStatusUpdate() {
      if (!this.ordin && !this.activi) {
        this.$emit('update')
      }
    }
  }
}
</script>
